$(function () {
    //初始化数据源表格
    initDataSourceTable();
    //初始化事件
    initEvent();
});




function initDataSourceTable() {
    var options = {
        pagination: true,
        url : "/datasource/page",
        columns : [
            {checkbox : true},
            {field : "uid",title : "UID",align:"center",visible:false},
            {field : "namespace",title : "空间",align:"center"},
            {field : "name",title : "名字",align:"center"},
            {field : "code",title : "编号",align:"center"},
            {field : "databaseType",title : "数据库类型",align:"center"},
            {field : "databaseName",title : "数据库名",align:"center"},
            {field : "username",title : "用户名",align:"center"},
            {field : "operate",title : "操作",align:"center",formatter : function (value, row, index) {
                    return "<button type='button' class='btn btn-link edit' index='"+index+"'>编辑</button>" +
                        "<button type='button' class='btn btn-link detail' index='"+index+"'>详情</button>";
                }},
        ],
        queryParams : function(params){
            var form = $("#dataSourceSearch").serializable();
            return $.extend({},params || {},form);
        },
        responseHandler : function (response) {
            return {total : response.total ,rows : response.records};
        }
    };

    $("#dataSourceTable").bootstrapTable(options);
}


function initEvent() {
    //查询
    $("#dataSourceSearch").on("click",".search",function (event) {
        $("#dataSourceTable").bootstrapTable("refresh");
    });
    //新增
    $("#dataSourceSearch").on("click",".add",function (event) {
        openDataSourceEditDialog("新增数据源");
    });
    //编辑
    $("#dataSourceTable").on("click",".edit",function (event) {
        var data =  $("#dataSourceTable").bootstrapTable("getData");
        var index = $(this).attr("index");
        openDataSourceEditDialog("修改数据源",data[index]);
    });
    //详情
    $("#dataSourceTable").on("click",".detail",function (event) {
        var data =  $("#dataSourceTable").bootstrapTable("getData");
        var index = $(this).attr("index");
        BootstrapDialog.show({
            title: "数据源详情",
            cssClass: "drawer right",
            url: "/pages/dds/dataSourceDetail.html",
            success: function () {
                $("#datasourceData").data("datasourceData", data[index]);
            },
        });
    })
}

function openDataSourceEditDialog(title,data) {
    BootstrapDialog.show({
        title : title,
        cssClass : "drawer right",
        url: "/pages/dds/dataSourceEdit.html",
        success : function(){
            $("#datasourceData").data("datasourceData",data);
        },
        buttons: [
            {
                id: 'dataSource-btn-ok',
                label: '确定',
                cssClass: 'btn-info',
                action: function(dialog) {
                    saveDataSource();
                    dialog.close();
                    $("#dataSourceTable").bootstrapTable("refresh");
                },
            },
            {
                id: 'dataSource-btn-secondary',
                label: '取消',
                cssClass: 'btn-light',
                action: function(dialog) {
                    dialog.close();
                },
            }
        ],
    });
}